package com.hicoffee.bm.controller;


import com.hicoffee.bm.model.TransactionDetails;
import com.hicoffee.bm.service.TransactionDetailsService;
import com.hicoffee.bm.utils.ResponseUtil;

import org.apache.ibatis.annotations.ResultMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import com.radarwin.framework.web.controller.BaseController;
import com.radarwin.framework.web.response.ResponseWrapper;
import com.radarwin.framework.util.StringUtil;


/**
 * auto generated by generation tools v1.0.0
 */
@Controller
@RequestMapping("/transaction")
public class TransactionDetailsController extends BaseController {
    private static final Logger LOGGER = LogManager.getLogger(TransactionDetailsController.class);


    @Autowired
    private TransactionDetailsService transactionDetailsService;


    @RequestMapping(value = "/pageList")
    @ResponseBody
    public ResponseWrapper getTransactionDetailsPageList(HttpServletRequest request) {
        return transactionDetailsService.getTransactionDetailsPageList(convertParamToMap(request));
    }


    @RequestMapping(value = "/get")
    @ResponseBody
    public ResponseWrapper get(String id) {
        TransactionDetails transactionDetails = new TransactionDetails();
        if (StringUtil.isNotBlank(id)) {
            transactionDetails = transactionDetailsService.selectByKey(id);
        }
        return ResponseUtil.successResponse("transactionDetails", transactionDetails);
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public ResponseWrapper create(TransactionDetails transactionDetails) {
        int result = transactionDetailsService.insert(transactionDetails);
        if (result > 0) {
            return ResponseUtil.successResponse("新增成功");
        }
        return ResponseUtil.successResponse("新增失败");
    }


    @RequestMapping(value = "/update")
    @ResponseBody
    public ResponseWrapper update(TransactionDetails transactionDetails) {
        int result = transactionDetailsService.update(transactionDetails);
        if (result > 0) {
            return ResponseUtil.successResponse("更新成功");
        }
        return ResponseUtil.successResponse("更新失败");
    }


    @RequestMapping(value = "/delete")
    @ResponseBody
    public ResponseWrapper delete(String ids) {
        TransactionDetails transactionDetails = new TransactionDetails();
        int result = 0;
        if (StringUtil.isNotBlank(ids)) {
            transactionDetails.setId(Long.valueOf(ids));
            transactionDetails.setDeleted(true);
            result = transactionDetailsService.update(transactionDetails);
        }
        if (result > 0) {
            return ResponseUtil.errorResponse("删除成功");
        }
        return ResponseUtil.errorResponse("删除失败");
    }


    @RequestMapping(value = "/commit")
    @ResponseBody
    public ResponseWrapper commit(@RequestBody String json) {
        try {
            LOGGER.info(json);
            return transactionDetailsService.saveTransaction(json);
        } catch (Exception e) {
            LOGGER.error(e);
            return ResponseUtil.errorResponse("error");
        }
    }


}
